layui.use(['form', 'table', 'layer', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate,
        table = layui.table;

    var sis = 1;
    table.render({
        elem: '#currentTableId',
        url: '../api/sort/uppxia',
        where: {
            sis: sis
        },
        request: {
            pageName: 'current' //页码的参数名称，默认：page
            , limitName: 'size' //每页数据量的参数名，默认：limit
        },
        cols: [[
            {field: 'sid', width: 330, title: '编号', sort: true, align: "center"},
            {field: 'sname', width: 330, title: '类别名称', align: "center"},
            {field: 'sremark', width: 330, title: '类别备注', align: "center"},
            {title: '操作', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true
    });


    $(".data-list-sort").click(function () {
        $("#currentTableBar").html(`<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">下架 </a>`)
        table.reload('currentTableId', {
            url: '../api/sort/findSortPage',

        });
    });

    //未下架类别
    $(".data-uppweixia-sort").click(function () {
        $("#currentTableBar").html(`<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">下架 </a>`)
        tableReload1();
    });

    function tableReload1() {
        var sis = 1;
        table.reload('currentTableId', {
            url: '../api/sort/uppxia',
            where: {
                sis: sis
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }

    //查询所有下架的类别
    function tableReload() {
        var sis = 0;
        table.reload('currentTableId', {
            url: '../api/sort/uppxia',
            where: {
                sis: sis
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }

    $(".data-uppxia-sort").click(function () {
        $("#currentTableBar").html(`<a class="layui-btn layui-btn-xs layui-btn-danger data-count-uppshang" lay-event="uppshang">撤销下架 </a>`)
        tableReload();
    });


    var sname = $("#sName").val()
    $.get({
        url: "http://localhost:8888/api/sort/sortname?name=" + sname,
        success: (resp) => {
            if (resp == "name") {
                $("#sName").html("用户名已存在").css("color", "red");
            } else {

            }

        }

    })

    // 监听添加操作
    $(".data-add-btn").on('click', function () {
        layer.open({
            type: 1,
            title: "添加",
            // btn:["","取消"],
            area: ["600px", "350px"],
            content: $("#window").html(),

            success: function (layero, index) {
            },
            yes: function () {
            }
        });
        form.render();//动态渲染

    });
    form.on('submit(sortAdd)', function (data) {
        var param = data.field;
        console.log(JSON.stringify(param));
        $.ajax({
            url: 'http://localhost:8888/api/sort/saveSort',
            method: 'post',
            data: data.field,
            dataType: 'json',
            success: function (res) {
                if (res.code = '0') {
                    parent.closeIframe(res.msg);
                } else
                    alert(res.msg);
            },
            error: function (data) {

            }
        });

    });

    // 监听删除操作（下架）
    $(".data-delete-btn").on("click", function () {
        var checkStatus = table.checkStatus('currentTableId')
            , data = checkStatus.data;
        console.log(data)

    });
    table.on('tool(currentTableFilter)', function (obj) {
        //得到所在行所有键值
        var sid = obj.data.sid;
        console.log(sid)
        if (obj.event === 'delete') {

            layer.confirm('确定要下架' + sname + '嘛', function (index) {
                obj.del();
                $.ajax({
                    url: 'http://localhost:8888/api/sort/editSort',
                    type: 'post',
                    data: {
                        sid: sid,
                    }, success: function (data) {

                    },
                    error: function () {

                    }
                });
                layer.close(index);
            });
        } else if (obj.event === 'uppshang') {
            layer.confirm('确定要上架' + sname + '嘛', function (index) {
                obj.del();
                $.ajax({
                    url: 'http://localhost:8888/api/sort/uppshangSort',
                    type: 'post',
                    data: {
                        sid: sid,
                    }, success: function (data) {

                    },
                    error: function () {

                    }
                });
                layer.close(index);
            });
        }
    })


});